package com.j2ee.spring.aop.aspect;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.j2ee.mapper.TbCallsMapper;
import com.j2ee.pojo.TbCalls;
import com.j2ee.pojo.TbCallsExample;
import com.j2ee.pojo.TbCallsExample.Criteria;
import com.j2ee.utils.SqlSessionFactoryUtil;

@Component("statisticsOfCalls")
public class StatisticsOfCalls {
	
	/**
	 * 前置通知
	 * @param joinPoint
	 */
	public void countsOfCall(JoinPoint joinPoint) {
		Object target = joinPoint.getTarget() ;
		String[] split = target.toString().split("@") ;
		String methodname = joinPoint.getSignature().getName() ;
		String layer = "" ;
		String classname = split[0] ;
		if(classname.contains("action")) {
			layer = "action" ;
		} else if (classname.contains("service")) {
			layer = "service" ;
		} else if (classname.contains("dao")) {
			layer = "dao" ;
		}
		SqlSession sqlSession = SqlSessionFactoryUtil.openSession() ;
		TbCallsMapper callsMapper = sqlSession.getMapper(TbCallsMapper.class) ;
		TbCallsExample example = new TbCallsExample() ;
		Criteria criteria = example.createCriteria() ;
		criteria.andClassnameLike(classname) ;
		criteria.andMethodnameLike(methodname) ;
		List<TbCalls> lists = callsMapper.selectByExample(example) ;
		if(lists.isEmpty() || lists == null) {
			TbCalls tbCalls = new TbCalls() ;
			tbCalls.setClassname(classname) ;
			tbCalls.setMethodname(methodname) ;
			tbCalls.setCount(1) ;
			tbCalls.setLayer(layer) ;
			callsMapper.insert(tbCalls) ;
			sqlSession.commit() ;
		} else {
			TbCalls tbCalls = lists.get(0) ;
			tbCalls.setCount(tbCalls.getCount() + 1) ;
			callsMapper.updateByPrimaryKey(tbCalls) ;
			sqlSession.commit() ;
		}
	}
	
	/**
	 * 后置通知
	 * @param joinPoint
	 * @throws Throwable
	 */
	public void exeTime(ProceedingJoinPoint joinPoint) throws Throwable {
		long begin = System.currentTimeMillis() ;
		//调用执行目标方法
		joinPoint.proceed() ;
		long end = System.currentTimeMillis() ;
		long exetime = end - begin ;
		//将执行时间写进数据库
		//1、查询数据库中是否存在改方法
		//	如果不存在，则添加该方法到数据库中，并填入调用执行时间跟调用次数
		//	如果存在该记录，则更新调用次数与对应的调用时间
		//查询条件：类名.方法名
		Object target = joinPoint.getTarget() ;
		String[] split = target.toString().split("@") ;
		String classname = split[0] ; //类名
		String methodname = joinPoint.getSignature().getName() ; //方法名
		String layer = "action" ; //action层中的方法
		SqlSession sqlSession = SqlSessionFactoryUtil.openSession() ;
		TbCallsMapper callsMapper = sqlSession.getMapper(TbCallsMapper.class) ;
		TbCallsExample example = new TbCallsExample() ;
		Criteria criteria = example.createCriteria() ;
		criteria.andClassnameLike(classname) ;
		criteria.andMethodnameLike(methodname) ;
		List<TbCalls> lists = callsMapper.selectByExample(example) ;
		if(lists.isEmpty() || lists == null) {
			TbCalls tbCalls = new TbCalls() ;
			tbCalls.setClassname(classname) ;
			tbCalls.setMethodname(methodname) ;
			tbCalls.setCount(1) ;
			tbCalls.setLayer(layer) ;
			tbCalls.setExetime(exetime + ",") ;
			callsMapper.insert(tbCalls) ;
			sqlSession.commit() ;
		} else {
			TbCalls tbCalls = lists.get(0) ;
			tbCalls.setCount(tbCalls.getCount() + 1) ;
			tbCalls.setExetime(tbCalls.getExetime() + exetime + ",") ;
			callsMapper.updateByPrimaryKey(tbCalls) ;
			sqlSession.commit() ;
		}
	}

}
